/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package codegen.glsl.ui;

import com.jme3.asset.AssetInfo;
import com.jme3.asset.AssetKey;
import com.jme3.asset.AssetLocator;
import com.jme3.asset.AssetManager;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

public class InMemoryLocator implements AssetLocator {

    public void setRootPath(String rootPath) {
        System.out.println(" " + rootPath);
    }

    public AssetInfo locate(AssetManager manager, AssetKey key) {
        //InMemoryAssetKey ikey = (InMemoryAssetKey) key;
        String realName = key.getName();
        realName = realName.replaceFirst("__inmemory::", "");
        realName = realName.replaceFirst(".foo", "");
        //String str = ikey.getData().getMap().get(realName);
        String str = MatPreviewApp.getMap().get(realName);
        if (str != null) {
            final InputStream is = new ByteArrayInputStream(str.getBytes());

            return new AssetInfo(manager, key) {
                @Override
                public InputStream openStream() {
                    return is;
                }
            };
        } else {
            throw new RuntimeException("There is no in memory asset this name!" + key.getName());
        }
    }
}
